/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.streampark.console.core.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/** log client */
@Slf4j
@Component
public class LogClientService {
  public String rollViewLog(String path, int skipLineNum, int limit) {
    String result = "";
    try {
      List<String> lines = readPartFileContent(path, skipLineNum, limit);
      StringBuilder builder = new StringBuilder();
      lines.forEach(line -> builder.append(line).append("\r\n"));
      return builder.toString();
    } catch (Exception e) {
      log.error("roll view log error", e);
    }
    return result;
  }

  /**
   * read part file content，can skip any line and read some lines
   *
   * @param filePath file path
   * @param skipLine skip line
   * @param limit read lines limit
   * @return part file content
   */
  private List<String> readPartFileContent(String filePath, int skipLine, int limit) {
    File file = new File(filePath);
    if (file.exists() && file.isFile()) {
      try (Stream<String> stream = Files.lines(Paths.get(filePath))) {
        return stream.skip(skipLine).limit(limit).collect(Collectors.toList());
      } catch (IOException e) {
        log.error("read file error", e);
      }
    } else {
      log.info("file path: {} not exists", filePath);
    }
    return Collections.emptyList();
  }
}
